#include "RenzoPrecompiled.h"
#include "RenzoDynLib.h"

namespace Renzo {
DynLib::DynLib(const char* name)
{
	this->name = name;
}

DynLib::~DynLib(void)
{
}

void DynLib::load() {
	Log::instance()->write("Loading dynamic library %s", name);

	/*
	 * Win32 DLL load
	 */
    hinstLib = LoadLibraryA(name); // use non-Unicode version
 
    // if the handle is valid, try to get the function address.
    if (hinstLib == 0) 
	{
		rzWarning(ERROR_DLL_LOAD, name);
	}
}

void DynLib::unload() {
	rzAssert(hinstLib != 0);
	Log::instance()->write("Unloading dynamic library %s", name);
	// free the DLL module.
    bool fFreeResult = FreeLibrary(hinstLib);
}

const char* DynLib::getName() {
	return name;
}

void* DynLib::getSymbol(const char* symbol) const {
	rzAssert(hinstLib != 0);

	void* proc = GetProcAddress(hinstLib, symbol);
    if (proc == 0) 
    {
        rzWarning(ERROR_DLL_SYMBOL_NOT_FOUND, symbol, name);
    }
	return proc;
}

}